列表(List)是Python中使用频率最高的数据结构:
列表基于动态数组(Dynamic Array)实现:
| 特性 | 技术实现 | 时间复杂度 | 金融应用场景 |
|---|---|---|---|
| 可变性 | 元素直接修改 | O(1) | 实时价格更新 |
| 有序性 | 维护插入顺序 | - | 时间序列数据 |
| 异构性 | 存储任意类型 | - | 混合数据记录 |
| 动态性 | 自动扩容 | amortized O(1) | 增量数据采集 |
# 注:该代码块包含未完成的填空代码,需要在平台上完成
# 注意:此答案不全,请查阅平台。
# ⚠️ 平台原始代码 - 请原样输入至教学平台(注释除外),平台才会判定答案正确
#任务一
name_index = ["道琼斯工业平均指数","富时100指数","标普500指数","恒生指数","日经225指数","上证指数","深证指数"] #创建指数名称列的列表
price_index = [41563.08,8376.63,5648.40,17989.07,38647.75,2842.21,8348.48] # 定义列表price_index
print(name_index[2]) #访问 "标普500指数" 这个元素
print(price_index.index(8376.63)) #找出 8376.63 这个元素所在的索引值
#任务二
name_index = ["道琼斯工业平均指数","富时100指数","标普500指数","恒生指数","日经225指数","上证指数","深证指数"]
price_index = [41563.08,8376.63,5648.40,17989.07,38647.75,2842.21,8348.48] # 定义列表price_index
name_index.append("法国CAC40指数") #按要求添加新元素
name_index.append("德国DAX指数") # 将股指名称添加到列表
name_index.append("新加坡海峡指数") # 将股指名称添加到列表
name_index.append("台湾加权指数") # 将股指名称添加到列表
print(name_index) #打印添加新元素后的name_index列表
price_index.append(7630.95) #按要求添加新元素
price_index.append(18906.92) # 将股指收盘点数添加到列表
price_index.append(3442.93) # 将股指收盘点数添加到列表
price_index.append(22268.09) # 将股指收盘点数添加到列表
print(price_index) #打印添加新元素后的price_index列表
#任务三
name_index = ["道琼斯工业平均指数","富时100指数","标普500指数","恒生指数","日经225指数","上证指数","深证指数"]
price_index = [41563.08,8376.63,5648.40,17989.07,38647.75,2842.21,8348.48] # 定义列表price_index
price_index.remove(38647.75) #删除"日经225指数"这个元素
price_index.insert(6,2674.45) #添加索引为6的"韩国综合指数"这个元素
print(name_index) # 输出指数数据
print(price_index) # 输出价格数据
#任务四
name_index = ["道琼斯工业平均指数","富时100指数","标普500指数","恒生指数","日经225指数","上证指数","深证指数"]
price_index = [41563.08,8376.63,5648.40,17989.07,38647.75,2842.21,8348.48] # 定义列表price_index
price_index.sort() #将price_index列表元素由小到大排序
print(price_index) # 输出价格数据
price_index.reverse() #将price_index列表元素翻转
print(price_index) # 输出价格数据
price_index.clear() #删除price_index列表全部元素(使用clear函数)
print(price_index) # 输出价格数据| 操作 | 时间复杂度 | 说明 |
|---|---|---|
lst[i] |
O(1) | 索引访问 |
lst.append(x) |
amortized O(1) | 末尾追加 |
lst.insert(i, x) |
O(n) | 任意位置插入 |
lst.pop() |
O(1) | 弹出末尾 |
lst.pop(0) |
O(n) | 弹出首元素 |
lst.remove(x) |
O(n) | 删除指定值 |
排序: [1, 1, 2, 3, 4, 5, 6, 9]
反转: [9, 6, 5, 4, 3, 2, 1, 1]
| 方法 | 原地修改 | 返回值 | 使用场景 |
|---|---|---|---|
list.sort() |
是 | None | 不需要保留原列表 |
sorted(list) |
否 | 新列表 | 需要保留原顺序 |
# 定义持仓股票组合
# 列表+字典的嵌套结构,适合表示"实体-属性"关系
portfolio = [
{'code': '600519.SH', 'name': '贵州茅台', 'shares': 100, 'price': 1850.00},
{'code': '000858.SZ', 'name': '五粮液', 'shares': 200, 'price': 220.50},
{'code': '600036.SH', 'name': '招商银行', 'shares': 500, 'price': 45.20}
]
total_value = 0
for stock in portfolio:
value = stock['shares'] * stock['price']
total_value += value
print(f"{stock['name']}: {value:,.2f}元")
print(f'\n总投资: {total_value:,.2f}元')贵州茅台: 185,000.00元
五粮液: 44,100.00元
招商银行: 22,600.00元
总投资: 251,700.00元
portfolio = [
{'code': '600519.SH', 'name': '贵州茅台', 'shares': 100, 'price': 1850.00},
{'code': '000858.SZ', 'name': '五粮液', 'shares': 200, 'price': 220.50},
{'code': '600036.SH', 'name': '招商银行', 'shares': 500, 'price': 45.20}
]
# 计算总价值
total_value = sum(s['shares'] * s['price'] for s in portfolio)
# 计算每只股票的权重
weights = []
for stock in portfolio:
value = stock['shares'] * stock['price']
weight = value / total_value
weights.append(weight)
print(f"{stock['name']}: {weight:.2%}")
print(f'权重总和: {sum(weights):.4f}')贵州茅台: 73.50%
五粮液: 17.52%
招商银行: 8.98%
权重总和: 1.0000
对于n个元素的列表:
相比之下,NumPy数组只需约 8 KB(紧凑存储)
| 需求场景 | 推荐数据结构 | 原因 |
|---|---|---|
| 频繁添加/删除 | 列表 | 动态数组 |
| 频繁头部操作 | collections.deque |
双端队列 O(1) |
| 快速查找 | 字典/集合 | 哈希表 O(1) |
| 大规模数值计算 | NumPy数组 | 内存紧凑 |
list.append() 而非 list + [x](后者创建新列表)[None] * nextend() 批量添加,比循环 append() 更高效[商业大数据分析与应用]